<?php

namespace Rebecca\RebeccaBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Rebecca\RebeccaBundle\Entity\PeriodosEnConceptos;
use Rebecca\RebeccaBundle\Form\PeriodosEnConceptosType;

/**
 * PeriodosEnConceptos controller.
 *
 */
class PeriodosEnConceptosController extends Controller
{
    /**
     * Lists all PeriodosEnConceptos entities.
     *
     */
    public function indexAction()
    {
        /* @var $component Rebecca\RebeccaBundle\Component\PeriodosEnConceptosComponent */
        $component = $this->get("components.PeriodosEnConceptos");
        $entities = $component->getAll();

        return $this->render('RebeccaBundle:PeriodosEnConceptos:index.html.twig', array(
            'entities' => $entities
        ));
    }

    /**
     * Finds and displays a PeriodosEnConceptos entity.
     *
     */
    public function showAction($id)
    {
        /* @var $component Rebecca\RebeccaBundle\Component\PeriodosEnConceptosComponent */
        $component = $this->get("components.PeriodosEnConceptos");

        $entity = $component->get($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find PeriodosEnConceptos entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return $this->render('RebeccaBundle:PeriodosEnConceptos:show.html.twig', array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Displays a form to create a new PeriodosEnConceptos entity.
     *
     */
    public function newAction()
    {
        $id = $this->getRequest()->get('idPeriodo');
        /* @var $component Rebecca\RebeccaBundle\Component\PeriodosComponent */
        $component = $this->get("components.Periodos");
        
        /* @var $periodo \Rebecca\RebeccaBundle\Entity\Periodo */
        $periodo = $component->get($id);
        
        /* @var $componentPeriodosEnConceptos Rebecca\RebeccaBundle\Component\PeriodosEnConceptosComponent */
        $componentPeriodosEnConceptos = $this->get("components.PeriodosEnConceptos");
        
        $conceptos = $componentPeriodosEnConceptos->getConceptos($id);
        
        if(!isset ($conceptos)){
            $conceptos = null;
        }
        
        $entity = new PeriodosEnConceptos();

        return $this->render('RebeccaBundle:PeriodosEnConceptos:new.html.twig', array(
            'entity'     => $entity,
            'conceptos'  => $conceptos,
            'periodo'    => $periodo
        ));
    }

    /**
     * Creates a new PeriodosEnConceptos entity.
     *
     */
    public function createAction()
    {
        /* @var $component Rebecca\RebeccaBundle\Component\PeriodosEnConceptosComponent */
        $component = $this->get("components.PeriodosEnConceptos");
        
        $request = $this->getRequest();
        
        $AllConceptos = $request->get("AllConceptos");
        $idPeriodo = $request->get("idPeriodo");
        
        $conceptos = array();
        
        if(!isset ($AllConceptos)){
            $conceptos = $request->get("conceptos");
        }else{
            foreach($component->getConceptos($idPeriodo) as $concepto){
                array_push($conceptos, $concepto->getId());
            }
        }
        
        $component->massiveSave($conceptos, $idPeriodo);

        return $this->redirect($this->generateUrl('periodos'));
    }

    /**
     * Displays a form to edit an existing PeriodosEnConceptos entity.
     *
     */
    public function editAction($id)
    {
        /* @var $component Rebecca\RebeccaBundle\Component\PeriodosEnConceptosComponent */
        $component = $this->get("components.PeriodosEnConceptos");
        $entity = $component->get($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find PeriodosEnConceptos entity.');
        }

        $editForm = $this->createForm(new PeriodosEnConceptosType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        return $this->render('RebeccaBundle:PeriodosEnConceptos:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Edits an existing PeriodosEnConceptos entity.
     *
     */
    public function updateAction($id)
    {
        /* @var $component Rebecca\RebeccaBundle\Component\PeriodosEnConceptosComponent */
        $component = $this->get("components.PeriodosEnConceptos");

        $entity = $component->get($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find PeriodosEnConceptos entity.');
        }

        $editForm   = $this->createForm(new PeriodosEnConceptosType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        $request = $this->getRequest();

        $editForm->bindRequest($request);

        if ($editForm->isValid()) {
            $component->update($entity);
            $component->commit();

            return $this->redirect($this->generateUrl('periodosenconceptos'));
        }

        return $this->render('RebeccaBundle:PeriodosEnConceptos:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Deletes a PeriodosEnConceptos entity.
     *
     */
    public function deleteAction($id)
    {
        $form = $this->createDeleteForm($id);
        $request = $this->getRequest();

        $form->bindRequest($request);

        if ($form->isValid()) {
            /* @var $component Rebecca\RebeccaBundle\Component\PeriodosEnConceptosComponent */
            $component = $this->get("components.PeriodosEnConceptos");
            $entity = $component->get($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find PeriodosEnConceptos entity.');
            }

            $component->delete($entity);
            $component->commit();
        }

        return $this->redirect($this->generateUrl('periodosenconceptos'));
    }

    private function createDeleteForm($id)
    {
        return $this->createFormBuilder(array('id' => $id))
            ->add('id', 'hidden')
            ->getForm()
        ;
    }
}
